package com.briup.util;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.function.Function;

/**
 * JDBC 工具类
 */
public class JDBCUtil {
    private static Properties p = new Properties();
    static DataSource dataSource;

    static {
        //读取配置文件
        InputStream stream = JDBCUtil.class.getClassLoader().getResourceAsStream("druid.properties");
        try {
//            将文件读取到集合
            p.load(stream);
            //创建连接池
            dataSource = DruidDataSourceFactory.createDataSource(p);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //获取数据库连接  通过数据连接池
    public static Connection getConnection() {
        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static PreparedStatement getStatement(String sql) {
        try {
            return getConnection().prepareStatement(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Statement getStatement() {
        try {
            return getConnection().createStatement();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return  null;
    }
    public  static  void executeDDL(String sql){
        PreparedStatement ps = getStatement(sql);
        try {
            ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static  int executeDML(String sql){
        PreparedStatement ps = getStatement(sql);
        try {
            return  ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return  0;
    }
    public  static<T> List<T> executeDQL(String sql, Function<ResultSet,T> function){
       List<T> list = new ArrayList<>();
        PreparedStatement ps = getStatement(sql);
        try {
            ResultSet resultSet = ps.executeQuery();
            while(resultSet.next()){
                T t = function.apply(resultSet);
                list.add(t);
            }
            return  list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static  void close(Connection connection, Statement statement){
        close(connection,statement,null);
    }
    static void close(Connection connection, Statement statement, ResultSet resultSet){
        if (connection!=null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (statement!=null) {
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (resultSet!=null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
